package cn.hairui.blog.mapper.manage;

import cn.hairui.blog.model.manage.MgrResc;
import cn.hairui.blog.model.manage.MgrRescmenu;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author lihairui
 * @version V1.0
 * @date: 2019/8/4 11:58
 */
@Mapper
public interface ResourceMapper {

    @Select("SELECT * FROM MGR_RESC ORDER BY ORDER_ID ASC")
    List<MgrResc> listRescMgr();

    @Select("SELECT * FROM MGR_RESC WHERE USEFLAG='on' ORDER BY ORDER_ID ASC")
    List<MgrResc> listRescMgrShow();

    @Select("SELECT * FROM MGR_RESCMENU WHERE RESC_ID=#{rescId} ORDER BY ORDER_ID ASC")
    List<MgrRescmenu> findMgrRescmenuByrescId(String rescId);

    @Select("SELECT RESC_ID AS rescId,RESC_NAME AS rescName,ORDER_ID AS orderId, CASE WHEN USEFLAG='on' THEN '是' ELSE '否' END AS useflag FROM MGR_RESC ORDER BY ORDER_ID ASC")
    List<Map> listMgrRescMgr();

    @Insert("INSERT INTO MGR_RESC " +
            "(RESC_ID,RESC_NAME,ORDER_ID,RESC_ICON,USEFLAG) " +
            "VALUES" +
            "(#{rescId},#{rescName},#{orderId},#{rescIcon},#{useflag})")
    void addMgrResc(MgrResc mgrResc);

    @Select("SELECT * FROM MGR_RESC WHERE RESC_ID=#{rescId}")
    MgrResc findMgrResc(String rescId);

    @Update("UPDATE MGR_RESC SET RESC_NAME=#{rescName},ORDER_ID=#{orderId},USEFLAG=#{useflag} WHERE RESC_ID=#{rescId}")
    void updateMgrResc(MgrResc mgrResc);

    @Select("select * from(select resc_id as permissionId,resc_name as permissionName,'' as permissionUrl, '0' as pid,order_id as seq,'0' as resType from mgr_resc order by order_id asc) a " +
            "union all " +
            "select * from(select title as permissionId,order_id as permissionName,href as permissionUrl,resc_id as pid,'' as seq,'1' as rescType  from mgr_rescmenu  order by order_id asc) b")
    List<Map> listMgrRescmenuMgr();

    @Select("select * from mgr_rescmenu where resc_id=#{rescId} and href=#{href}")
    MgrRescmenu findMgrRescmenu(String rescId, String href);

    @Insert("INSERT INTO MGR_RESCMENU " +
            "(RESC_ID,HREF,TITLE,ICON,ORDER_ID,SPREAD,TARGET) " +
            "VALUES" +
            "(#{rescId},#{href},#{title},#{icon},#{orderId},#{spread},#{target})")
    void addMgrRescmenu(MgrRescmenu mgrRescmenu);

    @Update("UPDATE MGR_RESCMENU SET TITLE=#{title},ICON=#{icon},ORDER_ID=#{orderId} WHERE RESC_ID=#{rescId} AND HREF=#{href}")
    void updateMgrRescmenu(MgrRescmenu mgrRescmenu);

    @Delete("DELETE FROM MGR_RESCMENU WHERE RESC_ID=#{rescId} AND HREF=#{href}")
    void deleteMgrRescmenu(String rescId, String href);

    @Delete("DELETE FROM MGR_RESC WHERE RESC_ID=#{rescId}")
    void deleteMgrResc(String rescId);
}
